package com.py.credit.utils;

import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author YaoS
 * @program project2.6
 * @classname RestTemplateUtils
 * @description 调用restTemplate
 * @date 2019-01-17 19:04
 */
public class RestTemplateUtils {

    /**
     * 功能描述: 发送test请求
     *
     * @param url 请求url "http://ip:post/link?param1={name1}&param2={name2}"
     * @param map 请求参数 "{name1:"test1",name2:"test2"}"
     * @return 请求"http://ip:post/link?param=test1&param2=test2"的结果
     * @author YaoS
     * @date 19/1/21 15:14
     */
    public static ResponseEntity<String> sendGet(String url, Map<String, String> map){
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForEntity(url, String.class, map);
    }
    /**
     * 功能描述: 发送Post请求
     *
     * @param
     * @return
     * @author YaoS
     * @date 19/1/17 19:07
     */
    public static ResponseEntity<String> sendPost(String url, MultiValueMap<String, Object> params) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(
                new MappingJackson2HttpMessageConverter());
        restTemplate.setRequestFactory(
                new HttpComponentsClientHttpRequestFactory());
        return restTemplate.postForEntity(url, params, String.class);
    }
    
    public static byte[] getBytes(String url, Map<String, Object> params) {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, byte[].class, params );
    }
    
    public static void uploadFile(byte[] buffer, String filename, HttpServletRequest request, HttpServletResponse response) {
        try {
            // 清空response
            response.reset();
            if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
                // firefox浏览器
                filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
            } else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
                // 谷歌
                filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
            } else {
                // IE浏览器
                filename = URLEncoder.encode(filename, "UTF-8");
            }
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + filename);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream;charset=UTF-8");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();

        }
    }
    
    public static void readFile(byte[] buffer, HttpServletResponse response) {
        try {
            response.setContentType("image/png");
            response.setCharacterEncoding("UTF-8");
            OutputStream outputSream = response.getOutputStream();
            InputStream in = new ByteArrayInputStream(buffer);
            in.close();
            int len;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf, 0, 1024)) != -1) {
                outputSream.write(buf, 0, len);
            }
            outputSream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
